package com.sunlg.smarthome.ui.fire;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.data.entity.FireDevice;
import com.sunlg.smarthome.data.entity.Warning;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;

import java.util.List;

/**
 * Created by sunlg on 2017/5/24.
 */

public class FireDeviceAdapter extends RecyclerView.Adapter implements View.OnClickListener{
    private List<FireDevice> deviceList;
    private Context mContext;
    private SHServiceWrapper mSHServiceWrapper;
    private OnItemClickListener onItemClickListener;

    public FireDeviceAdapter(Context context, List<FireDevice> deviceList) {
        this.deviceList = deviceList;
        mContext = context;
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_security_device, parent, false);
        view.setOnClickListener(this);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;

        viewHolder.itemView.setTag(position);  //设置tag，在onClick时取出

        FireDevice device = deviceList.get(position);

        viewHolder.tvDesc.setText(device.getDescription());
        if(device.getType() == 0){
            viewHolder.tvDeviceType.setText("烟雾");
        }else if(device.getType() == 1){
            viewHolder.tvDeviceType.setText("火焰");
        }

        mSHServiceWrapper.getLatestFireWarning(device.getId(), new MySubscriber<Result<Warning>>() {
            @Override
            public void onSuccess(Result<Warning> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    Warning warning = data.getData();
                    viewHolder.tvDetail.setText(warning.getDescription()+" "+warning.getTime());
                }else {
                    viewHolder.tvDetail.setText("");
                }
            }

            @Override
            public void _onError(Throwable e) {
                e.printStackTrace();
            }
        });

    }

    @Override
    public int getItemCount() {
        return deviceList.size();
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{
        TextView tvDesc;
        TextView tvDetail;
        TextView tvDeviceType;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_desc);
            tvDetail = (TextView)itemView.findViewById(R.id.tv_detail);
            tvDeviceType = (TextView)itemView.findViewById(R.id.tv_device_type);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    @Override
    public void onClick(View v) {
        this.onItemClickListener.onItemClick(v, (int)v.getTag());
    }
}
